<template>
  <el-container class="exchange">
    <el-main>
      <div class="card-list">
         <el-card shadow="never" class="tableCard">
          <vxe-toolbar :refresh="{query: getList1}">
            <template v-slot:buttons>
              <el-button type="primary" icon="el-icon-plus" @click="openDialog('', false)">新增小票格式</el-button>
            </template>
          </vxe-toolbar>
          <vxe-table
            ref="xTree"
            border
            row-key
            resizable
            highlight-hover-row
            :auto-resize="true"
            :data="list"
            :span-method="rowspanMethod"
            width
            max-height="500"
            row-id="id"
            highlight-current-row
            text-algin="center"
            :print-config="{}"
            header-align="center"
          >
            <vxe-table-column
              field="typeName"
              width="150"
              align="center"
              title="小票类型"
            />
            <vxe-table-column
              field="name"
              width="180"
              align="left"
              title="格式名称"
            />
            <vxe-table-column
              field="receiptTemplateName"
              width="160"
              align="left"
              title="打印模板"
            />
            <vxe-table-column
              field="printerEquipmentName"
              width="150"
              align="center"
              title="设备名称"
            />
            <vxe-table-column
              title="备注"
              field="remark"
              align="left"
              min-width="200"
            />
            <vxe-table-column
              title="状态"
              width="80"
              align="center"
              slots="default"
            >
              <template slot-scope="scope">
                <el-switch
                  v-model="scope.row.publishStatus"
                  :inactive-value="0"
                  :active-value="1"
                  active-text
                  inactive-text
                  @change="changeState(scope)"
                />
              </template>
            </vxe-table-column>
            <vxe-table-column
              title="操作"
              fixed="right"
              align="center"
              width="160"
            >
              <template slot-scope="scope">
                <div class="drag-btn">
                  <el-button
                    type="text"
                    size="mini"
                    @click="openDialog(scope.row.id, true)"
                  >查看</el-button>
                  <el-button
                    v-if="scope.row.publishStatus != 1"
                    type="text"
                    size="mini"
                    @click="openDialog(scope.row.id, false)"
                  >编辑</el-button>
                  <el-button
                    v-if="scope.row.publishStatus != 1"
                    type="text"
                    size="mini"
                    @click="delClick(scope.row.id)"
                  >删除</el-button>
                </div>
              </template>
            </vxe-table-column>
          </vxe-table>
        </el-card>
        <el-dialog :close-on-click-modal="false" :modal="false" :visible.sync="dialogVisible" title="设备绑定">
          <el-form ref="ruleForm" :model="params" label-width="7rem" :rules="rules">
            <el-row :gutter="40">
              <el-col :span="12">
                <el-form-item label="格式名称：" prop="name">
                  <el-input v-model="params.name" :disabled="disabledEdit" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="小票类型：" prop="type">
                  <el-select v-model="params.type" placeholder="请选择" :disabled="disabledEdit">
                    <el-option
                      v-for="item in typeList"
                      :key="item.key"
                      :label="item.name"
                      :value="item.key">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="打印模板："  prop="printTemplateId">
                  <el-select v-model="params.printTemplateId" placeholder="请选择" :disabled="disabledEdit">
                    <el-option
                      v-for="item in printTemplateList"
                      :key="item.id"
                      :label="item.templateName"
                      :value="item.id">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="设备名称："  prop="printerEquipmentId">
                  <el-select v-model="params.printerEquipmentId" placeholder="请选择" :disabled="disabledEdit">
                    <el-option
                      v-for="item in printerEquipmentList"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="备注：" prop="remark">
                  <el-input v-model="params.remark" type="textarea" :rows="2" :disabled="disabledEdit"/>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button v-if="!disabledEdit" type="primary" @click="submit">确 定</el-button>
          </div>
        </el-dialog>
      </div>
      </el-main>
  </el-container>
</template>

<script>
import {listPrintTemplate, listBillBindPrinterEquipment, viewBillBindPrinterEquipment, delBillBindPrinterEquipment,addBillBindPrinterEquipment,modifyBillBindPrinterEquipment, changeBillBindPrinterEquipmentStatus} from '@/api/print'
import {listPrinterEquipments, listEquipmentEnum } from '@/api/hardware'
export default {
  components: {
  },
  data() {
    return {
      title: '',
      disabledEdit: true,
      dialogVisible: false,
      flag: false,

      params:{
        name:'',
        type:'',
        printerEquipmentId:'',
        printTemplateId:'',
        remark:''
      },
      rules:{
        name: [{ required: true, message: '请输入格式名称', trigger: 'change' }],
        type: [{ required: true, message: '请选择小票类型', trigger: 'blur' }],
        printTemplateId: [{ required: true, message: '请选择小票模板', trigger: 'blur' }],
        printerEquipmentId: [{ required: true, message: '请选择打印机类型', trigger: 'blur' }]
      },
      list: [],
      typeList: [],
      printerEquipmentList: [],
      printTemplateList: []
    }
  },
  created() {
    this.getList()
    this.listAllEquipmentEnum()
    this.listPrinterEquipments()
    this.listPrintTemplate()
  },
  methods: {
    listPrinterEquipments(){
      listPrinterEquipments({}).then(res =>{
        this.printerEquipmentList = res
      })
    },
    listPrintTemplate(){
      listPrintTemplate({}).then(res =>{
        if(res){
          if(res.code){
            this.$message.error(`${res.msg}`);
          }else{
            this.printTemplateList = res
          }
        }
      })
    },
    listAllEquipmentEnum(){
      this.listEquipmentEnum('receiptType')
    },
    listEquipmentEnum(type){
      listEquipmentEnum(type).then(res =>{
        if(res){
          if(res.code){
            this.$message.error(`${res.msg}`);
          }else{
            if(type === 'receiptType'){
              this.typeList = res
            }
          }
        }
      })
    },
    refreshParams(params) {
      this.getList()
    },
    getList1(){
      this.getList()
    },
    // 列表
    getList() {
      listBillBindPrinterEquipment({}).then(res =>{
        if(res){
          if(res.code){
            this.$message.error(`${res.msg}`);
          }else{
            this.list = res
          }
        }
      })
    },

    // 详情
    openDialog(id, bool) {
       this.dialogVisible = true
       this.disabledEdit = bool
      if(id){
        this.flag = true
        viewBillBindPrinterEquipment(id).then(res =>{
          if(res){
            if(res.code){
              this.$message.error(`${res.msg}`);
            }else{
              this.params = res
            }
          }
        })
      }else{
        this.flag = false
        Object.keys(this.params).forEach(key => this.params[key] = '')
      }
    },
    // 删除
    delClick(id){
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delBillBindPrinterEquipment(id).then(res => {
          this.$message.success('删除成功!')
          this.getList()
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    changeState(scope) {
      this.list[scope.seq - 1].publishStatus =
        scope.row.publishStatus === 1 ? 0 : 1
      changeBillBindPrinterEquipmentStatus({
        id: scope.row.id,
        status: scope.row.publishStatus === 1 ? 0 : 1
      }).then((response) => {
        this.list[scope.seq - 1].publishStatus =
          scope.row.publishStatus === 1 ? 0 : 1
        this.$message.success(
          scope.row.publishStatus === 0 ? '禁用成功' : '启用成功'
        )
      })
    },
    // 保存
    submit(){
      if(this.flag){
        modifyBillBindPrinterEquipment(this.params).then(res=>{
          if(res){
            if(res.code){
              this.$message.error(`${res.msg}`);
            }else{
              this.$message.success('修改成功!')
              this.getList()
              this.dialogVisible = false
            }
          }
        })
      }else{
        addBillBindPrinterEquipment(this.params).then(res=>{
          if(res){
            if(res.code){
              this.$message.error(`${res.msg}`);
            }else{
              this.$message.success('保存成功!')
              this.getList()
              this.dialogVisible = false
            }
          }
        })
      }
    }
  }
}
</script>

<style>
</style>
